﻿using UnityEngine;
using System.Collections.Generic;
using BridgeShips;

namespace BridgeShips.AI
{
    public class StateData
    {
        public Dictionary<string, object> m_Dictionary { get; private set; }

        public StateData()
        {
            m_Dictionary = new Dictionary<string, object>();
        }

        public StateData(Dictionary<string, object> conditions)
        {
            m_Dictionary = conditions;
        }

        public void Add(string key, object value)
        {
            m_Dictionary.Add(key, value);
        }

        public void Clear()
        {
            m_Dictionary.Clear();
        }

        public static void OverrideValue(KeyValuePair<string, object> value, StateData data)
        {
            data.m_Dictionary[value.Key] = value.Value;
        }

        public static void OverrideValue(string key, object value, StateData data)
        {
            KeyValuePair<string, object> pair = new KeyValuePair<string, object>(key, value);

            OverrideValue(pair, data);
        }

        public static void OverrideValues(StateData overrider, StateData data)
        {
            foreach (KeyValuePair<string, object> pair in overrider.m_Dictionary)
                data.m_Dictionary[pair.Key] = pair.Value;
        }
    }

    public class HelpStrings
    {
        public class AI
        {
            // Dictionary
            public const string IS_SHIP_IN_SIGHT = "have linked Ship in sight";
            public const string CAN_LINK_SHIP = "Can Link Ship";
            public const string IS_SHIP_LINKED = "Ship is Linked";

            // Animator parameters.
            public const string ANIMATOR_PARAM_DRIVING = "Driving";
            public const string ANIMATOR_PARAM_LINKING = "Linking";

        }
    }

    public class AISettings : EntityBehaviour
    {
        public AIEntityMovement Movement { get { return m_Movement; } }
        public EntityDetection Detection { get { return m_Detection; } }

        public float BridgeShipLenght { get { return m_MaxBridgeShipLength; } }

        [SerializeField]
        private AIEntityMovement m_Movement;

        [SerializeField]
        private EntityDetection m_Detection;


        //0 分制式 1 带式
        [SerializeField]
        [Range(0, 1)]
        [Tooltip("Pattern of the Ship-（带式-分制式）")]
        private int m_ShipPattern = -1;

        [SerializeField]
		[Clamp(10f, 100f)]
		private float m_MaxBridgeShipLength = 37.0f;

        private AIShip m_Ship;

        private void Start()
        {
            m_Ship = GetComponent<AIShip>();
            m_Detection.Initialize(transform);
            m_Movement.Initialize( m_Ship );

        }
        public void UpdateShipPattern()
        {
            uint shipID = Detection.GetEnity().GetID();
            m_ShipPattern = (int)ShipsDictionary.Instance.shipPrefabricateType[shipID];
        }

        private void Update()
        {
           
            m_Detection.Update(m_Ship);
            m_Movement.Update( transform );
        }

        public ShipTypes GetShipPattern()
        {
            if (m_ShipPattern < 0)
            {
                UpdateShipPattern();
            }
            return (ShipTypes)m_ShipPattern;
        }

        public bool IsDone() {
            return m_Movement.ReachedDestination();
        }
    }
}